package cn.edu.sgu.www.mhxysy.service.fabao.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.fabao.FabaoCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.fabao.FabaoCategory;
import cn.edu.sgu.www.mhxysy.enums.FabaoTypes;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.fabao.FabaoCategoryPager;
import cn.edu.sgu.www.mhxysy.service.fabao.FabaoCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class FabaoCategoryServiceImpl implements FabaoCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final FabaoCategoryMapper fabaoCategoryMapper;

	@Autowired
	public FabaoCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			FabaoCategoryMapper fabaoCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.fabaoCategoryMapper = fabaoCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_FB;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(FabaoCategoryDTO fabaoCategoryDTO) {
		FabaoCategory fabaoCategory = toEntity(fabaoCategoryDTO);
		
		fabaoCategory.setId(null);

		//纠正门派法宝的类型
		if (fabaoCategory.getSchoolId() != null) {
			fabaoCategory.setType(FabaoTypes.MPFB.getValue());
		}

		fabaoCategoryMapper.insert(fabaoCategory);
	}

	@Override
	public void updateById(FabaoCategoryDTO fabaoCategoryDTO) {
		ObjectUtils.requireNonNull(fabaoCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		FabaoCategory fabaoCategory = toEntity(fabaoCategoryDTO);
		
		fabaoCategoryMapper.updateById(fabaoCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<FabaoCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				fabaoCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<FabaoCategory> selectAll() {
		QueryWrapper<FabaoCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.orderByAsc("type", "id");

		return fabaoCategoryMapper.selectList(queryWrapper);
	}

	@Override
	public FabaoCategory selectById(Integer id) {
		return fabaoCategoryMapper.selectById(id);
	}

	@Override
	public Page<FabaoCategory> selectByPage(FabaoCategoryPager pager) {
		QueryWrapper<FabaoCategory> queryWrapper = new QueryWrapper<>();
		Page<FabaoCategory> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getType() != null,
				"type", pager.getType()
		);
		queryWrapper.eq(
				pager.getSchoolId() != null,
				"school_id", pager.getSchoolId()
		);
		queryWrapper.like(
				StringUtils.isNotEmpty(pager.getName()),
				"name", pager.getName()
		);

		queryWrapper.orderByAsc("type");

		return fabaoCategoryMapper.selectPage(page, queryWrapper);
	}

	private FabaoCategory toEntity(FabaoCategoryDTO fabaoCategoryDTO) {
		FabaoCategory fabaoCategory = new FabaoCategory();
		
		fabaoCategory.setId(fabaoCategoryDTO.getId());
		fabaoCategory.setName(fabaoCategoryDTO.getName());
		fabaoCategory.setNote(fabaoCategoryDTO.getNote());
		fabaoCategory.setType(fabaoCategoryDTO.getType());
		fabaoCategory.setSchoolId(fabaoCategoryDTO.getSchoolId());
		fabaoCategory.setRelateType(fabaoCategoryDTO.getRelateType());
		fabaoCategory.setRelateGrade(fabaoCategoryDTO.getRelateGrade());

		return fabaoCategory;
	}

}